/**
 * Disable scrolling on iOS
 */
$(document).ready(function() {
	$(document).bind("touchmove", function(event) {
		event.preventDefault();
	});
});
//document.body.addEventListener('touchmove', function(event) {
//	event.preventDefault();
//}, false); 

$(document).ready(function() {
//	var css = $("<style type='text/css'></style>").appendTo("head");
//	css.append(".xdragignore { border: 1px solid red; }");
});

$(document).ready(function() {
	var dragId = null;
	var mouseDown = false;
	var position = { x: 0, y: 0, toString : function() { return "{"+ this.x +":"+ this.y +"}"; } };
	var dragInProgress = false;
	var dragSource = null;
	var dragElement = null;

	var dragCover = document.createElement("div");
	dragCover.id = "dragCover";
	$(dragCover).css("position", "absolute");
	$(dragCover).css("top", "0px");
	$(dragCover).css("left", "0px");
	$(dragCover).css("width", "100%");
	$(dragCover).css("height", "100%");
	$(dragCover).css("z-index", 1000);
	$(dragCover).css("cursor", "none");
	$(dragCover).css("overflow", "hidden");
	$(dragCover).css("pointer-events", "none");
	document.body.appendChild(dragCover);

	var dragImage = document.createElement("div");
	dragImage.id = "dragImage";
	$(dragImage).css("position", "absolute");
	$(dragCover).css("cursor", "none");
	$(dragCover).hide();
	$(dragCover).addClass("xdragignore");
	dragCover.appendChild(dragImage);

	var DataTransfer = {
		types : [],
		data : {},
		setData : function(mime, value) {
			this.data[mime] = value;
		},
		getData : function(mime) {
			return this.data[mime];
		},
		dropEffect : null,
		effectAllowed : null,
		files : [],
		setDragImage : function(image, x, y) {
			while (dragImage.hasChildNodes()) {
				dragImage.removeChild(dragImage.lastChild);
			}
			$(dragImage).css({ marginLeft : x +"px", marginTop : y +"px" });
			dragImage.appendChild(image);
		}
	};

//	event.dataTransfer.effectAllowed = 'move';

	function triggerDragEvent(type, source, target) {
		var event = $.Event(type);
		event.target = target;
		event.dataTransfer = DataTransfer;
		$(source).trigger(event);
	}

	function hasClassOrParent(node, className) {
		if (node == null) return false;
		if ($(node).hasClass(className)) return true;
		if (node.parentNode == null) return false;
		return hasClassOrParent(node.parentNode, className);
	}

	function dragTick(element) {
		if (!element) {
			element = getTopElementAtPoint(position.x, position.y)
		}

		triggerDragEvent("xdrag", dragSource, dragSource);

		if (element != dragElement) {
			if (dragElement) {
				triggerDragEvent("xdragleave", dragSource, dragElement);
			}
			if (hasClassOrParent(element, "xdroppable")) {
				dragElement = element;

				triggerDragEvent("xdragenter", dragSource, dragElement);
			} else {
				dragElement = null;
			}
		}
		if (dragElement) {
			triggerDragEvent("xdragover", dragSource, dragElement);
		}
	}

	function getTopElementAtPoint(x, y) {
		var element = document.elementFromPoint(x, y);
		var elements = getElementsAtPoint(x, y);

		while (hasClassOrParent(element, "xdragignore")) {
			element = elements.pop();
		}

		return element;
	}

	function getElementsAtPoint(x, y) {
		var result = [];

		function getElementsAtPointRec(parent, x, y) {
			for (c in parent.children) {
				var child = parent.children[c];
				if (!child.getBoundingClientRect) continue;
				var rect = child.getBoundingClientRect();
				if (rect.left < x && rect.right > x &&
					rect.top < y && rect.bottom > y) {
					result.push(child);
					getElementsAtPointRec(child, x, y);
				}
			}
		}

		getElementsAtPointRec(document.body, x, y);
		return result;
	}

/*	$(document).on("xmousedown", ".xdraggable", function(event) {
		event.stopPropagation();

		dragSource = getTopElementAtPoint(event.clientX, event.clientY);
		mouseDown = true;
		position.x = event.clientX;
		position.y = event.clientY;
	}); */
	$(document).on("xtouchstart", ".xdraggable", function(event) {
		event.stopPropagation();

		var touch = event.touches[0];
		dragSource = getTopElementAtPoint(touch.clientX, touch.clientY);
		mouseDown = true;
		position.x = touch.clientX;
		position.y = touch.clientY;
	});

/*	$(document).on("xmousemove", function(event) {
		if (!mouseDown) return;
		if (dragInProgress) return;

		if ((Math.abs(position.x - event.clientX) < 4) &&
		    (Math.abs(position.y - event.clientY) < 4)) return;

		dragInProgress = true;

		$(dragCover).show();
		$(dragImage).show();

		triggerDragEvent("xdragstart", dragSource, dragSource);
	
		dragTick(dragSource);
		dragId = window.setInterval(dragTick, 100);
	}); */
	$(document).on("xtouchmove", function(event) {
		if (!mouseDown) return;
		if (dragInProgress) return;

		var touch = event.touches[0]
		if ((Math.abs(position.x - touch.clientX) < 4) &&
		    (Math.abs(position.y - touch.clientY) < 4)) return;

		dragInProgress = true;

		$(dragCover).show();
		$(dragImage).show();

		triggerDragEvent("xdragstart", dragSource, dragSource);

		dragTick(dragSource);
		dragId = window.setInterval(dragTick, 100);
	});

/*	$(document).on("xmousemove", function(event) {
		if (!dragInProgress) return;

		position.x = event.clientX;
		position.y = event.clientY;

		$(dragImage).css({ "left": event.clientX, "top": event.clientY });

	//	dragTick(event.target);
	}); */
	$(document).on("xtouchmove", function(event) {
		if (!dragInProgress) return;

		var touch = event.touches[0];
		position.x = touch.clientX;
		position.y = touch.clientY;

		$(dragImage).css({ left: touch.clientX, top: touch.clientY });

//		dragTick(event.target);
	});

/*	$(document).on("vmouseup", function(event) {
		window.clearInterval(dragId);
		mouseDown = false;

		if (!dragInProgress) return;
		dragInProgress = false;
		$(dragImage).hide();
		$(dragCover).hide();

		if (hasClassOrParent(dragElement, "xdroppable")) {
			triggerDragEvent("xdrop", dragSource, dragElement);
			dragElement = null;
		}

		triggerDragEvent("xdragend", dragSource, dragSource);
		dragSource = null;
	}); */
	$(document).on("xtouchend", function(event) {
		window.clearInterval(dragId);
		mouseDown = false;

		if (!dragInProgress) return;
		dragInProgress = false;
		$(dragImage).hide();
		$(dragCover).hide();

		if (hasClassOrParent(dragElement, "xdroppable")) {
			triggerDragEvent("xdrop", dragSource, dragElement);
			dragElement = null;
		}

		triggerDragEvent("xdragend", dragSource, dragSource);
		dragSource = null;
	});
});

/**
 * Drag Events
 */
/*
var dragsource = null;
$(document).ready(function() {
	$(".xdraggable").bind("xdragstart", function(event) {
		dragsource = $(event.target);
	});
	$(".xdraggable").bind("xdrag", function(event) {
	});
	$(".xdraggable").bind("xdragend", function(event) {
	});
	$(".xdroppable").bind("xdragenter", function(event) {
		$(event.target).addClass("drop-target");
	});
	$(".xdroppable").bind("xdragover", function(event) {
	});
	$(".xdroppable").bind("xdragleave", function(event) {
		$(event.target).removeClass("drop-target");
	});
	$(".xdroppable").bind("xdrop", function(event) {
		$(event.target).removeClass("drop-target");
	});
});
*/
